﻿#r "System.Runtime.Serialization"

open System
open System.IO
open System.Runtime.Serialization

[<DataContract>]
type PathPair = {
    [<field: DataMember(Name = "Input")>]
    Input : string

    [<field: DataMember(Name = "Output")>]
    Output : string option
}

[<DataContract>]
type SettingsData = {
    [<field: DataMember(Name = "Minify")>]
    Minify : bool

    [<field: DataMember(Name = "CreateWrapper")>]
    CreateWrapper : bool
    
    [<field: DataMember(Name = "Paths")>]
    Paths  : list<string * string option>
    
    [<field: DataMember(Name = "SettingsLocation")>]
    SettingsLocation : System.Drawing.Point option
}

module Settings =
    
    let save (settings: SettingsData) =
        let serializer = Json.DataContractJsonSerializer(typeof<SettingsData>)
        let fileName = Path.GetTempFileName()
        use fs = File.OpenWrite fileName
        serializer.WriteObject(fs, settings)
        fileName
    
    let load fileName =
        if File.Exists fileName then
            let serializer = Json.DataContractJsonSerializer(typeof<SettingsData>)
            use fs = File.OpenRead fileName
            serializer.ReadObject(fs) :?> SettingsData
        else
            { Minify = false; CreateWrapper = true; Paths = []; SettingsLocation = None }
